package com.killer.texreader.Activity;

import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.killer.texreader.R;
import com.killer.texreader.adapter.stageAdapter;
import com.killer.texreader.util.Globals;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class selectFileActivity extends AppCompatActivity {

    private File sd;
    private List<Map<String, Object>> values;
    private stageAdapter adapter;
    private ListView listView;
    private String fileFullName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Globals.init(this);
        setContentView(R.layout.activity_select_file);
        listView = (ListView) findViewById(R.id.grid_view);
        values = new ArrayList<>();


        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            sd = Environment.getExternalStorageDirectory();
            loadFile(sd);
        } else {
            Toast.makeText(this, "没有SD卡", Toast.LENGTH_LONG).show();

        }

        if (values != null) {

            //Log.i("killer", "文件名" + values.size() +"                  "+values.get(0).get("fileName"));

        }

        adapter = new stageAdapter(this, values);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Globals.fullPath = (String) values.get(position).get("fileName");
                Log.i("killer","Globals.fullPath        "+Globals.fullPath);
               /* Intent intent = getIntent();
                intent.putExtra("msg",fileFullName);
                setResult(1,intent);*/
                finish();
                Intent intent = new Intent(selectFileActivity.this,MainActivity.class);
                // startActivityForResult(intent,1);
                startActivity(intent);
            }
        });



    }

    private void loadFile(File file) {
        File[] children = file.listFiles();
        if (children != null) {
            for (int i = 0; i < children.length; i++) {
                String fileName = children[i].getName();
                // System.out.println(children[i].getName());
                Map<String, Object> map = new HashMap<>();

                if (children[i].isDirectory()&&children[i].getPath().indexOf("/.") == -1 ) {
                    loadFile(children[i]);
                } else {
                    String lastName = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();

                    //Log.i("killer","文件名_________"+lastName);
                    if ("txt".equals(lastName)) {
                        map.put("fileName", file + "/" + fileName);
                        map.put("textName", fileName.substring(0,fileName.lastIndexOf(".")));
                        values.add(map);
                    }
                }
            }
        }
    }

    public String getFileFullName() {
        return fileFullName;
    }

    public void setFileFullName(String fileFullName) {
        this.fileFullName = fileFullName;
    }
}
